﻿using MediatR;
using RedDragon.Common.Domain.produceplan;
using RedDragon.Common.Infrastructure.Interface;
using RedDragon.RBAC.API.Application.Common.ProcessW;
using T.Common.Domain.Common;

namespace RedDragon.RBAC.API.Application.Common.produceplanW
{
    public class produceplanSearchHandler : IRequestHandler<produceplanSearchCommon, Result>
    {
        public readonly IBaseRepository<produceplan> produceplanRepository;

        public produceplanSearchHandler(IBaseRepository<produceplan> produceplanRepository)
        {
            this.produceplanRepository = produceplanRepository;
        }

        public async Task<Result> Handle(produceplanSearchCommon request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = 0, Msg = "" };
            try
            {
                var list = from a in produceplanRepository.Query()
                           where a.IsDelete == false
                           select new ProduceDto()
                           {
                               produceplanId = a.produceplanId,
                               produceplanNumber = a.produceplanNumber,
                               produceplanName = a.produceplanName,
                               produceplanDetail = a.produceplanDetail,
                               produceplanStatus = a.produceplanStatus,
                               produceplanStatusName = a.produceplanStatus == 1 ? "新增" : a.produceplanStatus == 2 ? "变更" : a.produceplanStatus == 2 ? "确认" : "取消",
                               MakeName = a.MakeName,
                               MakeDepartment = a.MakeDepartment,
                               MakeDepartmentName = a.MakeDepartment == 1 ? "开发部" : a.MakeDepartment == 2 ? "管理部" : "外交部",
                               CreateTime = a.CreateTime,
                               produceplanAudit = a.produceplanAudit,
                               produceplanAuditName = a.produceplanAudit == 0 ? "未提交" : a.produceplanAudit == 1 ? "已提交" : a.produceplanAudit == 2 ? "已驳回" : a.produceplanAudit == 3 ? "已审批" : "变更"
                           };
                if (request.produceplanNumber != null)
                {
                    list = list.Where(x => x.produceplanNumber == request.produceplanNumber);
                }
                if (request.produceplanName != null)
                {
                    list = list.Where(x => x.produceplanName == request.produceplanName);
                }
                if (request.produceplanStatus != -1)
                {
                    list = list.Where(x => x.produceplanStatus == request.produceplanStatus);
                }

                result.Data = list;
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }
    }
    public class ProduceDto
    {
        public int produceplanId { get; set; }
        /// <summary>
        /// 计划编码
        /// </summary>
        public string? produceplanNumber { get; set; }
        /// <summary>
        /// 计划名称
        /// </summary>
        public string? produceplanName { get; set; }
        /// <summary>
        /// 计划描述
        /// </summary>
        public string? produceplanDetail { get; set; }
        /// <summary>
        /// 状态
        /// </summary>
        public int produceplanStatus { get; set; }
        /// <summary>
        /// 状态名称
        /// </summary>
        public string produceplanStatusName { get; set; }
        /// <summary>
        /// 制单人
        /// </summary>
        public string? MakeName { get; set; }
        /// <summary>
        /// 制单部门id
        /// </summary>
        public int MakeDepartment { get; set; }
        /// <summary>
        /// 制单部门名称
        /// </summary>
        public string MakeDepartmentName { get; set; }
        /// <summary>
        /// 创建时间
        /// </summary>
        public DateTime CreateTime { get; set; }
        /// <summary>
        /// 审批状态
        /// </summary>
        public int produceplanAudit { get; set; }
        /// <summary>
        /// 审批状态名称
        /// </summary>
        public string produceplanAuditName { get; set; }
        /// <summary>
    }
}
